//
//  ViewController.m
//  19-Homework
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "ViewController.h"
#import "BrandList.h"
#import "List.h"
#import "CarsTableViewCell.h"
#import "HotBrandTableViewCell.h"
#import "ExtensionTableViewCell.h"
#define kTableViewReuse @"reuse"
#define kHotBrandTableViewCell @"brandReuse"
#define kExtenTableViewCell @"extensionReuse"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *carTableView;
@property (nonatomic, strong) NSMutableArray<BrandList *> *scetionArray;
@property (nonatomic, strong) NSMutableArray *indexArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scetionArray = [NSMutableArray array];
    self.navigationController.navigationBar.translucent = NO;
    [self getAllCarsDataFromNet];
    [self creatTableView];
}

- (void)getAllCarsDataFromNet {
    NSURL *url = [NSURL URLWithString:@"http://app.api.autohome.com.cn/autov4.6/cars/brands-a2-pm2-v4.6.1-ts635580073583652962.html"];
    NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
    mutableRequest.HTTPMethod = @"GET";
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       NSDictionary *dicJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
       NSDictionary *resultDic = [dicJSON objectForKey:@"result"];
       NSArray *brandListArray = [resultDic objectForKey:@"brandlist"];
       
       for (NSDictionary *dictionary in brandListArray) {
           BrandList *brandList = [BrandList modelWithDic:dictionary];
           [_scetionArray addObject:brandList];
       }
        self.indexArray = [NSMutableArray array];
        for (BrandList *brandList in _scetionArray) {
            [_indexArray addObject:brandList.letter];
        }
        [_indexArray insertObject:@"热" atIndex:0];
        [_indexArray insertObject:@"主" atIndex:1];
        [_carTableView reloadData];
   }];
    [dataTask resume];
}

- (void)creatTableView {
    self.carTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _carTableView.bounces = NO;
    _carTableView.delegate = self;
    _carTableView.dataSource = self;
    [self.view addSubview:_carTableView];
    [_carTableView registerClass:[HotBrandTableViewCell class] forCellReuseIdentifier:kHotBrandTableViewCell];
    [_carTableView registerClass:[ExtensionTableViewCell class] forCellReuseIdentifier:kExtenTableViewCell];
    [_carTableView registerNib:[UINib nibWithNibName:@"CarsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTableViewReuse];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 120;
    } else if (indexPath.section == 1) {
        return 100;
    } else {
        return 70;
    }
}
#pragma mark - TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _scetionArray.count + 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"热门品牌";
    } else if (section == 1) {
        return @"主打车";
    } else {
        return [_scetionArray[section - 2] letter];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return 1;
    } else {
        return [_scetionArray[section - 2] list].count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HotBrandTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kHotBrandTableViewCell];
        return cell;
    } else if (indexPath.section == 1) {
        ExtensionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kExtenTableViewCell];
        return cell;
    } else {
        CarsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
        List *list = [_scetionArray[indexPath.section - 2] list][indexPath.row];
        cell.list = list;
        return cell;
    }
}

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
 
    return _indexArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
